import { Platform } from 'react-native';

export interface BluetoothDeviceInfo {
  id: string;
  name: string;
  address: string;
  connected: boolean;
  rssi?: number;
}

export class BluetoothService {
  private static instance: BluetoothService;

  public static getInstance(): BluetoothService {
    if (!BluetoothService.instance) {
      BluetoothService.instance = new BluetoothService();
    }
    return BluetoothService.instance;
  }

  /**
   * 获取当前已连接的蓝牙设备信息
   * 
   * 注意：当前版本使用模拟数据，避免了蓝牙库的兼容性问题
   * 如需真实蓝牙功能，请：
   * 1. 安装稳定的蓝牙库（如 react-native-bluetooth-serial）
   * 2. 添加原生模块链接
   * 3. 替换此方法中的模拟数据为真实API调用
   */
  async getConnectedDevices(): Promise<BluetoothDeviceInfo[]> {
    try {
      // 添加短暂延迟模拟真实的API调用
      await new Promise(resolve => setTimeout(resolve, 1000));

      // 模拟已连接的蓝牙设备数据
      // 您可以修改这些数据来测试不同的设备信息显示效果
      const mockConnectedDevices: BluetoothDeviceInfo[] = [
        {
          id: 'esp32-001',
          name: 'ESP32-DevKit-V1',
          address: '24:0A:C4:12:34:56',
          connected: true,
          rssi: -42,
        },
        {
          id: 'esp32-002',
          name: 'ESP32-WROOM-32',
          address: '30:AE:A4:78:90:12',
          connected: true,
          rssi: -58,
        },
        {
          id: 'bluetooth-speaker',
          name: '蓝牙音响',
          address: '14:7D:DA:AB:CD:EF',
          connected: true,
          rssi: -35,
        }
      ];

      console.log(`模拟获取到 ${mockConnectedDevices.length} 个已连接的蓝牙设备`);
      return mockConnectedDevices;
    } catch (error: any) {
      console.error('获取已连接设备失败:', error);
      throw new Error(error.message || '获取蓝牙设备信息失败');
    }
  }
}

export default BluetoothService.getInstance();
